<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang@fistforward.com
// +----------------------------------------------------------------------



quanli_require('module/base/system/sysPromote.class.php');
quanli_require('module/base/system/itemHandler.class.php');

class ajax_promote {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch($_REQUEST['operFlg']) {                
                case 1://推广URL
                	$this->promote();
                	break;
                case 2://新增推广URL
                	$this->addPromote();
                	break;  
                case 3://修改状态为0
                	$this->savePromoteStatus();
                	break;
                case 4://推广URL的详细列表
                	$this->promoteUrlList();
                	break;     
                default:
                    $this->toReturn('参数错误');
                    break;

            }

        } else {

            $this->toReturn('参数错误');

        }

    }

    /**
     * 返回数据公共方法
     * @param string $message 提示信息
     * @param int $code 代码
     * @param mixed $data 其他数据
     */
    private function toReturn( $message, $code = 400, $data = null) {

        if (is_null($data)) {
            die( json_encode( array( 'code' => $code, 'message' => $message ) ) );
        } else {
            die( json_encode( array( 'code' => $code, 'message' => $message, 'data' => $data ) ) );
        }

    }


    //推广url显示页面
    public function promote() {

    	global $G_PGLOB;
    	
    	//实例查询类
    	$sysSearch = new sysPromote($G_PGLOB->DBCA);
    	
    	$sys_info_array = $sysSearch->getPromoteList( $_REQUEST );
   		
    	//组织返回的数据
    	$result_array = array(
    			'page' => $_REQUEST['page'],
    			'total' => $_REQUEST['count'],
    			'rows' => array()
    	);
   

            if ($sys_info_array) {
            foreach($sys_info_array as $item) {
            	
				//这里读取分类
            	
            	$sysItem = new itemHandler($G_PGLOB->DBCA);
            	$arg_array = array(
            		'itemID' =>$item['key1'],
            		'item' =>32			
            	);
            	
            	$sysItemResult = $sysItem->searchItemList($arg_array);
           		$key = $sysItemResult[0]->itemName.','.$item['key2'].','.$item['key3'];
                $result_array['rows'][] = array(
                		'id' => $item['promoteID'],
                    	'cell' 	=> array(
                    	'<a href="javascript: void(0);" onclick="coupon.yz(\''.$key.'\');">验证</a>',                    				
                    	"<a href=".$item['complete_url']." target=_blank>".$item['complete_url'].'</a>',		
                        $sysItemResult[0]->itemName ,
                        $item['key2'],
                        $item['key3'],
                        $item['url'],
                        $item['creator'],
                        $item['createTime'],
                        $item['start_time'],
                        $item['end_time']
                    )
                );
            }
        }
    		
    		echo json_encode($result_array);    	

    }
    
    
    // 新增推广URL
  	public function addPromote(){
  		global $G_SHOP;
  		$sys_promote = new entity( 'sys_promote' );
  		//先把key1分割成id和名字
		$key1= explode(',',trim($_POST['key1']));
  		$sys_promote->key1=$key1[0];
  		$sys_promote->key2=trim($_POST['key2']);
  		$sys_promote->key3=trim($_POST['key3']);
  		$sys_promote->url=trim($_POST['url']);
  		//首先判断这4个参数有没有重复
  		$result = $G_SHOP->DBCA->fetch($sys_promote);
  		
  		if($result == 1){

  			echo 2;
  			exit();
  		}
  		//首先判断有没有#
	  	if(strpos($sys_promote->url,'#')){
			//截取2段字符串用#分割
			$str1 = substr($sys_promote->url,0,strpos($sys_promote->url,'#'));
			$str2 = substr($sys_promote->url,strpos($sys_promote->url,'#'));
			$sys_promote->complete_url=$str1.'?key1='.$key1[1].'&key2='.$sys_promote->key2.'&key3='.$sys_promote->key3.$str2;
				
		}else{
			$sys_promote->complete_url=$sys_promote->url.'?key1='.$key1[1].'&key2='.$sys_promote->key2.'&key3='.$sys_promote->key3;
				
		}	
  		
  		//mod by allen.qiang 20150107 优化 start
  		$sys_promote->start_time=$_POST['start_time'] . " 00:00:00";
  		$sys_promote->end_time=$_POST['end_time'] . " 23:59:59";
        //mod by allen.qiang 20150107 优化 end
		$sys_promote->timeStamp = date('Y-m-d H:i:s',time());
        $sys_promote->creator = $G_SHOP->currentUserName;//操作人姓名
        $sys_promote->createTime = date('Y-m-d H:i:s',time());//操作人时间 
        if($G_SHOP->DBCA->save( $sys_promote ) ){
        	echo 1;
        	exit();
        }                           
  				
  		
  	}  
    
  	
    
  	//修改推广url的状态
  	public function savePromoteStatus(){
  		
  		global $G_SHOP;
  		//首先获取传递来的参数
  		$promoteID = $_POST['promoteID'];
		
  		//实例查询类
  		$sysSearch = new sysPromote($G_PGLOB->DBCA);
  		
  		$sys_info_array = $sysSearch->updateSave($promoteID);
  		
  		if($sys_info_array == 1){
  			echo 1;
  		}
  		
  	}
  	
  	//判断URL是否验证成功
  	public function promoteUrlList(){
  		global $G_SHOP;
  		$key = $_GET['key'];

  		$key_check = new sysPromote($G_PGLOB->DBCA);
  		$_REQUEST['key']=$key;
  		$key_check_result = $key_check->checkUrl($_REQUEST);
  		
  		//组织返回的数据
    	$result_array = array(
    			'page' => $_REQUEST['page'],
    			'total' => $_REQUEST['count'],
    			'rows' => array()
    	);
    	
    		
            if ($key_check_result) {
            foreach($key_check_result as $item) {
                $result_array['rows'][] = array(               		
                    	'cell' 	=> array(
                        $item['createTime'],
                        $item['access_url'],
                        $item['key1'],
                        $item['key2'],
                        $item['key3'],

                    )
                );
            }
        }
        
  		echo json_encode($result_array);    
  	}
  	
  	
}